package com.example.administrator.kaohedemo.View;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.example.administrator.kaohedemo.Action.CourseListAction;
import com.example.administrator.kaohedemo.Adapter.TypePopViewAdapter;
import com.example.administrator.kaohedemo.Model.CourseListBean;
import com.example.administrator.kaohedemo.R;
import com.example.administrator.kaohedemo.Utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/22.
 */
public class TypePopView extends PopupWindow{

    private static TypePopView typePopView;
    private static CourseListAction courseListAction;
    private TypePopViewAdapter typePopViewAdapter;
    private View view;
    private List<CourseListBean.Mbody.Type> segments=new ArrayList<CourseListBean.Mbody.Type>();
    private List<CourseListBean.Mbody.Type> stages=new ArrayList<CourseListBean.Mbody.Type>();
    private List<CourseListBean.Mbody.Type> studys=new ArrayList<CourseListBean.Mbody.Type>();
    private List<CourseListBean.Mbody.Type> types=new ArrayList<CourseListBean.Mbody.Type>();

    public static TypePopView getInstense(Context context){
        typePopView=new TypePopView(context);
        courseListAction=CourseListAction.getInstense();
        return typePopView;
    }

    public boolean GetIsShowing(){
        return this.isShowing();
    }

    public void SetDismiss(){
        this.dismiss();
    }

    public void setAdapterChanged(int position){
        switch (position){
            case 0:
                typePopViewAdapter.setList(segments);
                typePopViewAdapter.setMapKey("segments");
                break;
            case 1:
                typePopViewAdapter.setList(stages);
                typePopViewAdapter.setMapKey("stages");
                break;
            case 2:
                typePopViewAdapter.setList(studys);
                typePopViewAdapter.setMapKey("studys");
                break;
            case 3:
                typePopViewAdapter.setList(types);
                typePopViewAdapter.setMapKey("types");
                break;
        }
    }

    public void SetList(List<CourseListBean.Mbody.Type> list,int position){
        switch (position){
            case 0:
                if (segments.size()==0){
                    segments.addAll(list);
                }
                break;
            case 1:
                if (stages.size()==0){
                    stages.addAll(list);
                }
                break;
            case 2:
                if (studys.size()==0){
                    //2016要求
                    studys.addAll(list);
                }
                break;
            case 3:
                if (types.size()==0){
                    types.addAll(list);
                }
                break;
        }

    }

    public void SetTargetView(View view){
        this.view=view;
    }

    public void ShowAtLocation(View parent,int gravity,int x,int y){
        this.showAtLocation(parent,gravity,x,y);
    }

    public void ShowViewUp(){
        if (view==null)
            throw new IllegalArgumentException("targetview must not be null");
        this.showAtLocation(view, Gravity.TOP,0,0);
    }

    public void ShowViewUp(View parent){
        this.showAtLocation(parent, Gravity.TOP,0,0);
    }

    public void ShowViewDown(){
        if (view==null)
            throw new IllegalArgumentException("targetview must not be null");
        this.showAsDropDown(view);
    }

    public void ShowViewDown(View parent){
        this.showAsDropDown(parent);
    }

    private TypePopView(Context context){
        initview(context);
        setWidth(Utils.getScreenWidth(context));
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setAnimationStyle(R.style.Popup_down_anim);
        setOnDismissListener(onDismissListener);
    }

    private void initview(Context context) {
        LinearLayout view= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.pop_list,null);
        RecyclerView recyclerview= (RecyclerView) view.findViewById(R.id.recyclerview);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(OrientationHelper.VERTICAL);
        recyclerview.setLayoutManager(manager);
        typePopViewAdapter=new TypePopViewAdapter(context);
        recyclerview.setAdapter(typePopViewAdapter);
        this.setContentView(view);
    }

    private OnDismissListener onDismissListener=new OnDismissListener() {
        @Override
        public void onDismiss() {
            courseListAction.SendPopDismiss();
        }
    };

}
